//
//  ZDBPersonalCenterHeadView.m
//  ZDB
//
//  Created by Kk on 2020/3/17.
//  Copyright © 2020 ZDB. All rights reserved.
//

#import "ZDBPersonalCenterHeadView.h"
#import "ZDBTastViewController.h"
#import "ZDBInviteFriendsViewController.h"
#import "ZDBBindWXViewController.h"
#import "ZDBWithdrawalViewController.h"
#import "ZDBWithdrawalActionViewController.h"
//#import "ZDBPersonalViewController.h"
#import "YZAuthID.h"
@implementation ZDBPersonalCenterHeadView


-(void)awakeFromNib{
    [super awakeFromNib];
   
      
    KViewRadius(self.bgView, 6);
    KViewRadius(self.userIcon, 31);
    KViewRadius(_withdrawalBtn, _withdrawalBtn.height/2);
    _userIcon.contentMode = UIViewContentModeScaleAspectFill;
    KViewRadius(_userIcon, _userIcon.height/2);
   
   
    _accumulated_incomeValue.format= _today_incomeValue.format=  _integralValue.format= _balanceValue.format= @"%.2f";
    _accumulated_incomeValue.method = _today_incomeValue.method=   _integralValue.method= _balanceValue.method= UILabelCountingMethodEaseOut;
    _accumulated_incomeValue.unit = @"累计收入";
    _today_incomeValue.unit = @"总积分";
     _integralValue.unit = @"积分余额";
     _balanceValue.unit = @"账户余额";
     [self initCountingLabel];
    
    [_taskBut add_BtnClickHandler:^(NSInteger tag) {
        
        ZDBTastViewController * tastVC = [[ZDBTastViewController alloc] init];
        tastVC.title = @"我的任务";
        [[ZBDUnit currentViewController].navigationController pushViewController:tastVC animated:YES];
    }];
    [_teamBut add_BtnClickHandler:^(NSInteger tag) {
        ZDBInviteFriendsViewController * tastVC = [[ZDBInviteFriendsViewController alloc] init];
        tastVC.title = @"我的团队";
        tastVC.is_Pop = NO;
        [[ZBDUnit currentViewController].navigationController pushViewController:tastVC animated:YES];
    }];
    WeakSelf
    [_withdrawalBtn add_BtnClickHandler:^(NSInteger tag) { //登陆 提现 资料认证
        
        if (weakSelf.type ==0) {//登陆
            
            [[DJLoginHelper sharedInstance] dj_showLoginVC:[ZBDUnit currentViewController]];
            
        }else if (weakSelf.type ==1)//提现
        {
            


//            ZDBWithdrawalViewController * WithdrawalVC = [[ZDBWithdrawalViewController alloc] init];
//            [[ZBDUnit currentViewController].navigationController pushViewController:WithdrawalVC animated:YES];
            
                 ZDBWithdrawalActionViewController * WithdrawalVC = [[ZDBWithdrawalActionViewController alloc] init];
                 WithdrawalVC.title = @"我的钱包";
                 [[ZBDUnit currentViewController].navigationController pushViewController:WithdrawalVC animated:YES];
            
        }else //去认证资料
        {
            if ([DJLoginHelper sharedInstance].wx_Status.integerValue ==1) { //1=审核中 2=审核失败 3=审核通过
                return ;
            }else if ([DJLoginHelper sharedInstance].wx_Status.integerValue ==3)//去充值
            {
//                ZDBWithdrawalViewController * WithdrawalVC = [[ZDBWithdrawalViewController alloc] init];
//                WithdrawalVC.title = @"提现";
//                [[ZBDUnit currentViewController].navigationController pushViewController:WithdrawalVC animated:YES];
                
                ZDBWithdrawalActionViewController * WithdrawalVC = [[ZDBWithdrawalActionViewController alloc] init];
                             WithdrawalVC.title = @"我的钱包";
                             [[ZBDUnit currentViewController].navigationController pushViewController:WithdrawalVC animated:YES];
                
  
            }else
            {
                [self authVerification];
           
            }
          
        }
    }];
     UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(event:)];
    [tapGesture setNumberOfTapsRequired:1];
     [self addGestureRecognizer:tapGesture];
    
    
}

#pragma mark - 验证TouchID/FaceID
- (void)authVerification {
    
    YZAuthID *authID = [[YZAuthID alloc] init];
    
    [authID yz_showAuthIDWithDescribe:nil block:^(YZAuthIDState state, NSError *error) {
        
        if (state == YZAuthIDStateNotSupport) { // 不支持TouchID/FaceID
            NSLog(@"对不起，当前设备不支持指纹/面容ID");
            [JMNotifyView showNotify:@"对不起，当前设备不支持指纹/面容ID" isSuccessful:NO];
        } else if(state == YZAuthIDStateFail) { // 认证失败
            NSLog(@"指纹/面容ID不正确，认证失败");
             [JMNotifyView showNotify:@"指纹/面容ID不正确，认证失败" isSuccessful:NO];
        } else if(state == YZAuthIDStateTouchIDLockout) {   // 多次错误，已被锁定
            NSLog(@"多次错误，指纹/面容ID已被锁定，请到手机解锁界面输入密码");
             [JMNotifyView showNotify:@"指纹/面容ID不正确，认证失败" isSuccessful:NO];
        } else if (state == YZAuthIDStateSuccess) { // TouchID/FaceID验证成功
            NSLog(@"认证成功！");
               [JMNotifyView showNotify:@"认证成功！请完善审核资料" isSuccessful:YES];
               ZDBBindWXViewController * BindWXVC = [[ZDBBindWXViewController alloc] init];
              [[ZBDUnit currentViewController].navigationController pushViewController:BindWXVC animated:YES];
            
        }
        
    }];
}

- (void)initCountingLabel
{
    NSString * balanceStr = kFormat(@"%@\n%@", @"--",@"账户余额");
       _balanceValue.attributedText = [ZBDUnit finderattributedLinesString:balanceStr attributedString:@"--" color:WHITECOLOR font:AutoFont(16)];
        _balanceValue.textAlignment = NSTextAlignmentCenter;
       
       NSString * integralStr = kFormat(@"%@\n%@", @"--",@"积分余额");
       _integralValue.attributedText = [ZBDUnit finderattributedLinesString:integralStr attributedString:@"--" color:WHITECOLOR font:AutoFont(16)];
        _integralValue.textAlignment = NSTextAlignmentCenter;
       
       NSString * today_incomeStr = kFormat(@"%@\n%@", @"--",@"总积分");
       _today_incomeValue.attributedText = [ZBDUnit finderattributedLinesString:today_incomeStr attributedString:@"--" color:WHITECOLOR font:AutoFont(16)];
        _today_incomeValue.textAlignment = NSTextAlignmentCenter;
       
       NSString * accumulated_incomeStr = kFormat(@"%@\n%@", @"--",@"累计收入");
          _accumulated_incomeValue.attributedText = [ZBDUnit finderattributedLinesString:accumulated_incomeStr attributedString:@"--" color:WHITECOLOR font:AutoFont(16)];
       _accumulated_incomeValue.textAlignment = NSTextAlignmentCenter;
}
-(void)event:(UITapGestureRecognizer *)gesture
{
    if (_type ==0) {
        [[DJLoginHelper sharedInstance] dj_showLoginVC:[ZBDUnit currentViewController]];
    }
}
//type =0 未登陆 1已经登录资料通过审核 2审核状态
- (void)getUserHeadDataWithLoginType:(NSInteger)type
{
    _type = type;
    if (type ==0)
    {
        
        _userName.text = @"未登陆";
        _phoneLab.text = @"请登陆";
        _withdrawalBtn.hidden = _bgView.hidden = _setImage.hidden = YES;
        [_withdrawalBtn setTitle:@"登陆/注册" forState:0];
        self.height = 200;
       self.bottom_h.constant =0;
        self.userIcon.image = PlaceHolderImage;
         [self initCountingLabel];
    }else
    {
     
        self.bottom_h.constant =50;
         _withdrawalBtn.hidden = _bgView.hidden = _setImage.hidden = NO;
        
        NSDictionary * userDic = [[DJLoginHelper sharedInstance] cj_UserDicc];
        NSInteger viid = [userDic[@"viid"] integerValue];
        NSString * vip = @"rank_three2";
        if (viid==1) {
            vip = @"vip1";
        }else if (viid==2)
        {
            vip = @"vip2";
        }else if (viid==3)
        {
            vip = @"vip3";
        }else
        {
            vip = @"vip1";
        }
        NSString * userName = kFormat(@"%@   ", userDic[@"nikeName"]);
        _userName.attributedText = [ZBDUnit getcontent:userName image:vip index:userName.length imageframe:CGRectMake(0, -2, 16, 16)] ;
        _phoneLab.text = userDic[@"userName"];
        [self.userIcon sd_setImageWithURL:[NSURL URLWithString:[userDic jsonString:@"headImage"]] placeholderImage:PlaceHolderImage];
        
        if (_type==1) {
            
             [_withdrawalBtn setTitle:@"提现" forState:0];
        }else 
        {
            if ([DJLoginHelper sharedInstance].wx_Status.integerValue ==1) { //1=审核中 2=审核失败 3=审核通过
                 [_withdrawalBtn setTitle:@"审核中" forState:0];
            }else if ([DJLoginHelper sharedInstance].wx_Status.integerValue ==2)
            {
                 [_withdrawalBtn setTitle:@"审核失败" forState:0];
            }else if ([DJLoginHelper sharedInstance].wx_Status.integerValue ==3)
            {
                 [_withdrawalBtn setTitle:@"提现" forState:0];
            }else
            {
                 [_withdrawalBtn setTitle:@"去审核" forState:0];
            }
        }
        NSString * money = kFormat(@"%@", userDic[@"money"]);
         [_balanceValue countFrom:0 to:money.floatValue withDuration:1];
         _balanceValue.alignment = NSTextAlignmentCenter;
        
        NSString * sumIntegral = kFormat(@"%@", userDic[@"integral"]);
        [_integralValue countFrom:0 to:sumIntegral.floatValue withDuration:1];
        _integralValue.alignment = NSTextAlignmentCenter;
       
        NSString * today = kFormat(@"%@", userDic[@"sumIntegral"]);
        [_today_incomeValue countFrom:0 to:today.floatValue withDuration:1];
        _today_incomeValue.alignment = NSTextAlignmentCenter;
        
        NSString * total = kFormat(@"%@", userDic[@"sumMoney"]);
        [_accumulated_incomeValue countFrom:0 to:total.floatValue withDuration:1];
        _accumulated_incomeValue.alignment = NSTextAlignmentCenter;
    }
    
}
@end
